#include "../include/Plane.h"
#include <iostream>

Plane::Plane(Model *model, GLfloat x, GLfloat y, GLfloat z, GLuint shaderProg, sf::Clock *clock) {
    this->model = model;
    this->position.x = x;
    this->position.y = y;
    this->position.z = z;
    this->shaderProg = shaderProg;
    this->clock = clock;
    speed = 0;
    yaw = 0;
    pitch = 0;
    roll = 0;
    direction.x = 0;
    direction.y = 0;
    direction.z = 1;
}

void Plane::render(vec3 camPos) {
    glUseProgram(shaderProg);
    glPushMatrix();
    glDisable(GL_CULL_FACE);
    glTranslatef(position.x, position.y, position.z);
    //Calculate rotation
    glRotatef(roll, 0.0, 0.0, -1.0);
    glRotatef(yaw, 0.0, 1.0, 0.0);
    glRotatef(pitch, -1.0, 0.0, 0.0);
    model->render(shaderProg);
    glEnable(GL_CULL_FACE);
    glPopMatrix();
}

void Plane::update() {
    float time;
    if(clock == NULL) {
        time = 1;
    } else {
        time = clock->getElapsedTime().asSeconds();
    }
    position.x += sin((yaw/180)*M_PI)*(time*speed);
    position.y += sin((pitch/180)*M_PI)*(time*speed);
    position.z += cos((yaw/180)*M_PI)*(time*speed);
}

void Plane::adjustSpeed(GLfloat speed) {
    this->speed += speed;
    if(this->speed > maxSpeed) {
        this->speed = maxSpeed;
    } else if(this->speed < 0) {
        this->speed = 0;
    }
}

void Plane::adjustPitch(GLfloat angle) {
    pitch += angle;
    if(pitch < 0.0) {
        pitch += 360.0;
    } else if(pitch >= 360) {
        pitch -= 360.0;
    }
}

void Plane::adjustRoll(GLfloat angle) {
    roll += angle;
    if(roll < 0.0) {
        roll += 360.0;
    } else if(roll >= 360) {
        roll -= 360.0;
    }
}

void Plane::adjustYaw(GLfloat angle) {
    yaw += angle;
    if(yaw < 0.0) {
        yaw += 360.0;
    } else if(yaw >= 360) {
        yaw -= 360.0;
    }
}

vec3 Plane::getPosition() {
    return position;
}

void Plane::setPosition(vec3 pos) {
    position.x = pos.x;
    position.y = pos.y;
    position.z = pos.z;
}

GLfloat Plane::getPitch() {
    return pitch;
}

GLfloat Plane::getYaw() {
    return yaw;
}
